package com.control.qct.businesssteward.activity;

import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.TakeDeliveryOrderDetailAdapter;
import com.control.qct.businesssteward.adapter.TakeDeliveryOrderDetailSureAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.model.TakeDeliveryOrderDetailModel;
import com.control.qct.businesssteward.model.TakeDeliveryOrderDetailSureModel;
import com.control.qct.businesssteward.utils.DateUtil;
import com.control.qct.businesssteward.utils.DialogUtil;
import com.control.qct.businesssteward.utils.ScreenUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 收货单
 * Created by Administrator on 2018/1/17 0017.
 */

public class TakeDeliveryOrderActivity extends BaseActivity {

    @BindView(R.id.tv_receipts_code)
    TextView mTvReceiptsCode; //单据编号
    @BindView(R.id.tv_receipts_date)
    TextView mTvReceiptsDate;//单据日期
    @BindView(R.id.tv_take_delivery_date)
    TextView mTvTakeDeliveryDate;//收货日期
    @BindView(R.id.tv_replenishment_type)
    TextView mTvReplenishmentType;//补货分类
    @BindView(R.id.tv_operator)
    TextView mTvOperator;//经办人
    @BindView(R.id.tv_total_num)
    TextView mTvTotalNum;//总件数
    @BindView(R.id.tv_all_price)
    TextView mTvAllPrice;//总金额
    @BindView(R.id.rv_take_delivery_order_detail)
    RecyclerView mRvTakeDeliveryOrderDetail;//收货详情列表

    private List<TakeDeliveryOrderDetailModel> mReplenishmentOrderBeanList = new ArrayList<>();
    private TakeDeliveryOrderDetailAdapter mTakeDeliveryOrderDetailAdapter;
    private List<TakeDeliveryOrderDetailSureModel> mTakeDeliveryOrderDetailSureModelList = new ArrayList<>();

    public TakeDeliveryOrderActivity() {
        super(R.layout.activity_take_deliver_order);
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("收货单");
        for (int i = 0; i < 6; i++) {
            mReplenishmentOrderBeanList.add(new TakeDeliveryOrderDetailModel());
        }
        mRvTakeDeliveryOrderDetail.setLayoutManager(new LinearLayoutManager(this));
        mTakeDeliveryOrderDetailAdapter = new TakeDeliveryOrderDetailAdapter(mReplenishmentOrderBeanList);
        mRvTakeDeliveryOrderDetail.setAdapter(mTakeDeliveryOrderDetailAdapter);
        mTakeDeliveryOrderDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setTakeDeliveryDialog();
            }
        });
    }

    /**
     * 设置采购量弹窗
     */
    private void setTakeDeliveryDialog() {
        View takeDeliveryView = View.inflate(this, R.layout.dialog_take_delivery_order_num, null);
        final Dialog takeDeliveryDialog = DialogUtil.getDialog(this, true, takeDeliveryView, ScreenUtil
                        .getScreenWidthPix(this)
                , ScreenUtil
                        .getScreenHeightPix(this) * 0.8);
        ImageView ivGoodsPicture = takeDeliveryView.findViewById(R.id.iv_goods_picture);
        TextView tvGoodsName = takeDeliveryView.findViewById(R.id.tv_goods_name);
        TextView tvGoodsBarcode = takeDeliveryView.findViewById(R.id.tv_goods_barcode);
        TextView tvProcurementNum = takeDeliveryView.findViewById(R.id.tv_procurement_num);
        TextView tvGoodsPurchaseNum = takeDeliveryView.findViewById(R.id.tv_goods_purchase_num);
        final EditText etTakeDeliveryGoodsNum = takeDeliveryView.findViewById(R.id.et_take_delivery_goods_num);
        final EditText etTakeDeliveryComplimentaryNum = takeDeliveryView.findViewById(R.id
                .et_take_delivery_complimentary_num);
        RecyclerView rvTakeDeliveryOrderDetailSure = takeDeliveryView.findViewById(R.id
                .rv_take_delivery_order_detail_sure);
        takeDeliveryView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (takeDeliveryDialog != null) {
                    takeDeliveryDialog.dismiss();
                }
            }
        });
        takeDeliveryView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String goodsNum = etTakeDeliveryGoodsNum.getText().toString();
                String complimentaryNum = etTakeDeliveryComplimentaryNum.getText().toString();
                if (TextUtils.isEmpty(goodsNum)) {
                    showShortToast("请输入本次收货数");
                    return;
                }
                if (TextUtils.isEmpty(complimentaryNum)) {
                    showShortToast("请输入本次赠品数");
                    return;
                }
                mTakeDeliveryOrderDetailSureModelList.add(new TakeDeliveryOrderDetailSureModel("张三", Float.parseFloat
                        (goodsNum), Float.parseFloat(complimentaryNum), DateUtil.toDateHourFormat(System
                        .currentTimeMillis())));
                if (takeDeliveryDialog != null) {
                    takeDeliveryDialog.dismiss();
                }
            }
        });
        rvTakeDeliveryOrderDetailSure.setLayoutManager(new LinearLayoutManager(this));
        rvTakeDeliveryOrderDetailSure.setAdapter(new TakeDeliveryOrderDetailSureAdapter
                (mTakeDeliveryOrderDetailSureModelList));
    }

}
